package com.itheima.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.Result;
import com.itheima.item.domain.dto.GoodsQueryDTO;
import com.itheima.item.domain.po.Goods;
import com.itheima.item.domain.vo.PageVO;
import com.itheima.item.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/items")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/list")
    public Result<List<Goods>> list(@RequestHeader(value = "user-info",required = false) String userInfo) {
        System.out.println("item处的user-info:"+userInfo);
        List<Goods> list = goodsService.list();
        return Result.success(list);
    }
    @RequestMapping("/page")
    public Result<PageVO<Goods>> page(GoodsQueryDTO queryDTO) {
        Page<Goods> page = queryDTO.toMpPage();
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(queryDTO.getGoodsName() != null,Goods::getGoodsName, queryDTO.getGoodsName());
        Page<Goods> p = goodsService.page(page,wrapper);
        PageVO<Goods> pageVO = PageVO.of(p.getTotal(), p.getPages(), p.getRecords());
        return Result.success(pageVO);
    }
    @PostMapping("/add")
    public Result add(Goods goods) {
        goodsService.save(goods);
        return Result.success("添加成功");
    }
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        goodsService.deleteById(id);
        return Result.success("删除成功");
    }

}
